package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.CouponTemplate;
import com.example.mall.dto.CouponTemplateDto;
import com.example.mall.mapper.CouponTemplateMapper;
import com.example.mall.query.CouponTemplateQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ICouponTemplateService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import com.example.mall.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券模板表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class CouponTemplateServiceImpl extends ServiceImpl<CouponTemplateMapper, CouponTemplate> implements ICouponTemplateService {

    @Autowired
    private CouponTemplateMapper couponTemplateMapper;

    @Override
    public PageDTO<CouponTemplateDto> queryPage(CouponTemplateQuery couponTemplateQuery) {
        if (couponTemplateQuery == null) {
            couponTemplateQuery = new CouponTemplateQuery();
        }
        Page<CouponTemplate> page = couponTemplateQuery.toMpPageByDefaultCreateTimeAsc();
        Page<CouponTemplate> cartPage = lambdaQuery()
                .like(couponTemplateQuery.getTemplateId()!= null, CouponTemplate::getTemplateId, couponTemplateQuery.getTemplateId())
                .like(couponTemplateQuery.getType()!= null, CouponTemplate::getType, couponTemplateQuery.getType())
                .like(couponTemplateQuery.getRuleJson()!= null, CouponTemplate::getRuleJson, couponTemplateQuery.getRuleJson())
                .like(couponTemplateQuery.getPlatform()!= null, CouponTemplate::getPlatform, couponTemplateQuery.getPlatform())
                .like(couponTemplateQuery.getSourceType()!= null, CouponTemplate::getSourceType, couponTemplateQuery.getSourceType())
                .like(couponTemplateQuery.getTotal()!= null, CouponTemplate::getTotal, couponTemplateQuery.getTotal())
                .like(couponTemplateQuery.getRemaining()!= null, CouponTemplate::getRemaining, couponTemplateQuery.getRemaining())
                .like(couponTemplateQuery.getValidType()!= null, CouponTemplate::getValidType, couponTemplateQuery.getValidType())
                .like(couponTemplateQuery.getApplyRange()!= null, CouponTemplate::getApplyRange, couponTemplateQuery.getApplyRange())
                .like(couponTemplateQuery.getCouponStatus()!= null, CouponTemplate::getCouponStatus, couponTemplateQuery.getCouponStatus())
                .like(StringUtils.isNotEmpty(couponTemplateQuery.getTitle()), CouponTemplate::getTitle, couponTemplateQuery.getTitle())
                .like(StringUtils.isNotEmpty(couponTemplateQuery.getValidDays()), CouponTemplate::getValidDays, couponTemplateQuery.getValidDays())
                .apply(couponTemplateQuery.getValidStartTime() != null,
                        "DATE_FORMAT(valid_start_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        couponTemplateQuery.getValidStartTime())
                .apply(couponTemplateQuery.getValidEndTime() != null,
                        "DATE_FORMAT(valid_end_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        couponTemplateQuery.getValidEndTime())
                .orderByDesc(CouponTemplate::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, CouponTemplateDto.class);
    }

    @Override
    public CouponTemplate queryCouponTemplateById(Long templateId) {
        AssertUtil.isNotNull(templateId, "templateId不能为空");
        return couponTemplateMapper.selectById(templateId);
    }

    @Override
    public List<CouponTemplate> queryCouponTemplateList() {
        return this.list();
    }

    @Override
    public void insert(CouponTemplateDto couponTemplateDto) {
        CouponTemplate couponTemplate = new CouponTemplate();
        CopyUtils.copyProperties(couponTemplateDto, couponTemplate);
        couponTemplate.setTemplateId(CodeGenerateUtils.generateUniqueId());
        couponTemplate.setCreateDate(new Date());
        couponTemplate.setUpdateDate(new Date());
        couponTemplate.setCouponStatus(BaseConstants.EFFECTIVE_STATUS);
        couponTemplateMapper.insert(couponTemplate);
    }

    @Override
    public void update(CouponTemplateDto couponTemplateDto) {
        AssertUtil.isNotNull(couponTemplateDto.getTemplateId(), "templateId不能为空");
        CouponTemplate couponTemplate = couponTemplateMapper.selectById(couponTemplateDto.getTemplateId());
        AssertUtil.isNotNull(couponTemplate, "优惠券模板不存在");
        CopyUtils.copyProperties(couponTemplateDto, couponTemplate);
        couponTemplate.setUpdateDate(new Date());
        couponTemplateMapper.updateById(couponTemplate);
    }

    @Override
    public void delete(Long templateId) {
        AssertUtil.isNotNull(templateId, "templateId不能为空");
        CouponTemplate couponTemplate = couponTemplateMapper.selectById(templateId);
        AssertUtil.isNotNull(couponTemplate, "优惠券模板不存在");
        couponTemplate.setCouponStatus(BaseConstants.LOSE_STATUS);
        couponTemplate.setUpdateDate(new Date());
        couponTemplateMapper.updateById(couponTemplate);
    }

    @Override
    public void deleteBatch(List<Long> templateIds) {
        AssertUtil.isNotNull(templateIds, "templateIds不能为空");
        List<CouponTemplate> couponTemplates = couponTemplateMapper.selectBatchIds(templateIds);
        AssertUtil.isNotNull(couponTemplates, "优惠券模板不存在");
        for (CouponTemplate couponTemplate : couponTemplates) {
            couponTemplate.setCouponStatus(BaseConstants.LOSE_STATUS);
            couponTemplate.setUpdateDate(new Date());
            couponTemplateMapper.updateById(couponTemplate);
        }
    }
}
